// 例子15

{
    // 应用1：过滤HTML字符串，防止用户输入恶意内容
    function SaferHTML(templateData) {
        let s = templateData[0];
        for (let i = 1; i < arguments.length; i++) {
        let arg = String(arguments[i]);

        // Escape special characters in the substitution.
        s += arg.replace(/&/g, "&amp;")
                .replace(/</g, "&lt;")
                .replace(/>/g, "&gt;");

        // Don't escape special characters in the template.
        s += templateData[i];
        }
        return s;
    }

    let sender = '<script>alert("abc")</script>'; // 恶意代码
    let message = SaferHTML`<p>${sender} has sent you a message.</p>`;
    console.log(message); // <p>&lt;script&gt;alert("abc")&lt;/script&gt; has sent you a message.</p>
}

console.log('----------------------------------');

{
    // 应用2：多语言转换（国际化处理）
    try {
        i18n`Welcome to ${siteName}, you are visitor number ${visitorNumber}!` // "欢迎访问xxx, 您是第xxx位访问者！"
    } catch(e) {
        console.log('伪代码')
    }

}

console.log('----------------------------------');

{
    // 应用3：对其他语言/结构进行转换
    // 下面的代码通过jsx函数，将DOM字符串转换为React对象
    try {
        jsx`
            <div>
                <input
                    ref='input'
                    onchange='${this.handleChange}'
                    defaultValue = '${this.state.value}' />
                    ${this.state.value}
            </div>
        `
    } catch(e) {
        console.log('伪代码')
    }

}